﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FisheryPlatform.DataAccess.DBUtility;
using System.Data;
using FisheryPlatform.Entity;
using System.Data.SqlClient;

namespace FisheryPlatform.DataAccess.ShareCode
{
    public class ShareCodeDB
    {
        #region private const
        private const string PARAM_CODE_I = "@CodeID";
        private const string PARAM_CLASS_I = "@ClassID";
        private const string PARAM_NAME = "@Name";
        private const string PARAM_NAME_EN = "@NameEn";
        private const string PARAM_PARENT_ID = "@ParentId";
        private const string PARAM_CREATE_DATE = "@CreateDate";
        private const string PARAM_LAST_UPDATE_DATE = "@LastUpdateDate";

        private static readonly string ConnectionString = SQLHelper.CONNECTION_STRING;
        #endregion

        private static void FillEntityFrom(SqlDataReader reader, ShareCodeEntity obj)
        {
            #region
            if (reader != null && !reader.IsClosed)
            {
                obj.CodeID = reader.IsDBNull(reader.GetOrdinal("CodeID")) ? 0 : reader.GetInt64(reader.GetOrdinal("CodeID"));
                obj.ClassID = reader.IsDBNull(reader.GetOrdinal("ClassID")) ? 0 : reader.GetInt64(reader.GetOrdinal("ClassID"));
                obj.Name = reader.IsDBNull(reader.GetOrdinal("Name")) ? String.Empty : reader.GetString(reader.GetOrdinal("Name"));
                obj.NameEn = reader.IsDBNull(reader.GetOrdinal("NameEn")) ? String.Empty : reader.GetString(reader.GetOrdinal("NameEn"));
                obj.ParentId = reader.IsDBNull(reader.GetOrdinal("ParentId")) ? 0 : reader.GetInt64(reader.GetOrdinal("ParentId"));
                obj.CreateDate = reader.IsDBNull(reader.GetOrdinal("CreateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("CreateDate"));
                obj.LastUpdateDate = reader.IsDBNull(reader.GetOrdinal("LastUpdateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("LastUpdateDate"));
            }
            #endregion
        }

        private static SqlParameter[] GetShareCodeParams(ShareCodeEntity obj)
        {
            #region
            SqlParameter[] dbParams ={					
					 SQLHelper.MakeParam(PARAM_CODE_I, SqlDbType.BigInt,0,obj.CodeID),
					 SQLHelper.MakeParam(PARAM_CLASS_I, SqlDbType.BigInt,0,obj.ClassID),
					 SQLHelper.MakeParam(PARAM_NAME, SqlDbType.NVarChar, 100,obj.Name),					
					 SQLHelper.MakeParam(PARAM_NAME_EN, SqlDbType.NVarChar, 100,obj.NameEn),					
					 SQLHelper.MakeParam(PARAM_PARENT_ID, SqlDbType.BigInt,0,obj.ParentId)
			};

            return dbParams;
            #endregion
        }

        public static bool Save(ShareCodeEntity obj)
        {
            #region
            try
            {
                SqlParameter[] dbParams = GetShareCodeParams(obj);

                if (obj.CodeID == 0)
                {
                    obj.CodeID = Convert.ToInt32(SQLHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "ShareCode_InsertUpdate", dbParams));
                    return obj.CodeID > 0 ? true : false;
                }
                else
                {
                    var affectNum = SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "ShareCode_InsertUpdate", dbParams);
                    return affectNum > 0 ? true : false;
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCode-->InsertOrUpdate-->" + e.Message);

                ExceptionMessageDB.Record(ex);
            }

            return false;
            #endregion
        }

        public static ShareCodeEntity GetByShareCodeId(long codeID)
        {
            #region
            SqlDataReader reader = null;
            ShareCodeEntity obj = new ShareCodeEntity();

            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_CODE_I, SqlDbType.Int, 0, codeID)
                    };

                reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "ShareCode_GetByShareCodeId", dbParams);

                if (reader.Read())
                {
                    FillEntityFrom(reader, obj);
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCode-->GetSingleObject-->" + e.Message);

                ExceptionMessageDB.Record(ex);
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();
            }

            return obj;
            #endregion
        }

        public static bool Delete(long codeID)
        {
            #region
            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_CODE_I, SqlDbType.Int, 0, codeID)
                    };

                return SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "ShareCode_Delete", dbParams) > 0 ? true : false;
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCode-->Delete-->" + e.Message);

                ExceptionMessageDB.Record(ex);
            }
            return false;
            #endregion
        }

        public static List<ShareCodeEntity> Search(string sqlWhere, int pageIndex, int pageSize, out int pageCount)
        {
            #region
            List<ShareCodeEntity> result = new List<ShareCodeEntity>();
            pageCount = 0;
            try
            {
                if (string.IsNullOrEmpty(sqlWhere))
                {
                    sqlWhere = " 1=1 ";
                }

                SqlParameter[] dbParams =
                {
                    SQLHelper.MakeParam("@sqlWhere", SqlDbType.NVarChar,0,sqlWhere),
                    SQLHelper.MakeParam("@pageIndex",SqlDbType.Int,0,pageIndex),
                    SQLHelper.MakeParam("@pageSize",SqlDbType.Int,0,pageSize),
                    SQLHelper.MakeOutParam("@pageCount", SqlDbType.Int,0,0)                    
                };
                using (SqlDataReader reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "ShareCode_Search", dbParams))
                {
                    while (reader.Read())
                    {
                        ShareCodeEntity model = new ShareCodeEntity();
                        FillEntityFrom(reader, model);
                        result.Add(model);
                    }
                }
                pageCount = int.Parse(dbParams[3].Value.ToString());
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCode-->Search-->" + e.ToString());
                ExceptionMessageDB.Record(ex);
            }
            return result;
            #endregion
        }

        public static List<ShareCodeEntity> GetByClassId(long classId)
        {
            #region
            SqlDataReader reader = null;
            List<ShareCodeEntity> resultList = new List<ShareCodeEntity>();

            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_CLASS_I, SqlDbType.BigInt, 0, classId)
                    };

                reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "ShareCode_GetByClassId", dbParams);

                while (reader.Read())
                {
                    ShareCodeEntity obj = new ShareCodeEntity();
                    FillEntityFrom(reader, obj);

                    resultList.Add(obj);
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCode-->GetByClassId-->" + e.Message);

                ExceptionMessageDB.Record(ex);
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();
            }

            return resultList;
            #endregion
        }

        public static List<ShareCodeEntity> GetShareCodeListByClassIdAndPId(long classId, long parentId)
        {
            var sqlWhere = "classid=" + classId + " and ParentId=" + parentId;
            var pageCount = 0;
            return Search(sqlWhere, 0, 0, out pageCount);
        }
    }
}
